package com.fescoservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fescoservice.config.PropertiesConfig;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Link;
import com.fescoservice.service.LinkService;

@Controller
@RequestMapping("/action/Link.htm")
public class LinkController {

	private static final Logger log = LogManager.getLogger(LinkController.class);
	
	@Autowired
	private LinkService linkService;
		
	@RequestMapping(params = "cmd=addLink")
	public String addLink(Link link,String linkDept, ModelMap map,	HttpServletRequest request) throws FSException {

		String language = (String)request.getSession().getAttribute("userLanguage");
		link.setLinkLanguage(language);
		//link.setLinkDept(operDept);
		linkService.addLink(link);		
		getListModel(map,language,linkDept,1);
		return "manage/linklist";
	}
	
	@RequestMapping(params = "cmd=modifyLink")
	public String modifyLink(Link link,Integer linkId,String linkDept, ModelMap map,	HttpServletRequest request) throws FSException {
		Link oLink = linkService.getLink(linkId);
		String language = (String)request.getSession().getAttribute("userLanguage");
		oLink.setLinkTitle(link.getLinkTitle());
		oLink.setLinkUrl(link.getLinkUrl());
		linkService.updateLink(oLink);		
		getListModel(map,language,linkDept,1);
		return "manage/linklist";
	}

	@RequestMapping(params = "cmd=deleteLink")
	public String deleteLink(Integer linkId,String linkDept,ModelMap map,	HttpServletRequest request) throws FSException {

		String language = (String)request.getSession().getAttribute("userLanguage");
		linkService.deleteLink(linkId);
		getListModel(map,language,linkDept,1);
		return "manage/linklist";
	}


	@RequestMapping(params = "cmd=list1")
	public String listLink4Manage(String page,ModelMap map, HttpServletRequest request) throws FSException{
		String language = (String)request.getSession().getAttribute("userLanguage");
		String dept = "CERT";
		int pageNumber = Integer.parseInt((null==page || "".equals(page))?"1":page);
		getListModel(map,language,dept,pageNumber);

		return "manage/linklist";
	}
	
	@RequestMapping(params = "cmd=list2")
	public String listADLink4Manage(String page,ModelMap map, HttpServletRequest request) throws FSException{
		String language = (String)request.getSession().getAttribute("userLanguage");
		String dept = "DEPARTURE";
		int pageNumber = Integer.parseInt((null==page || "".equals(page))?"1":page);
		getListModel(map,language,dept,pageNumber);
		
		return "manage/linklist";
	}
	
	private ModelMap getListModel(ModelMap map, String language,String dept,int pageNumber) throws FSException{
		String linkType = "CERT".equals(dept)?"1":"2";
		Integer counts  = linkService.countLinks(language,dept);
		int first = PropertiesConfig.getInstance().getInt("ListSize.Operator")*(pageNumber-1);
		List list = linkService.listLinks(language,dept,first);
		map.addAttribute("linklist", list);
		map.addAttribute("counts", counts);
		map.addAttribute("pageNumber", pageNumber);
		map.addAttribute("linkDept", dept);
		map.addAttribute("linkType", linkType);
		return map;
	}
	
}
